package com.xcm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xcm.dao.DeviceInfoMapper;
import com.xcm.model.TbDeviceInfo;
import com.xcm.model.TbMachine;
import com.xcm.model.dto.DeviceGroupDTO;
import com.xcm.service.DeviceInfoService;

/**
 * 终端Service
 */
@Service(value = "deviceInfoService")
public class DeviceInfoServiceImpl implements DeviceInfoService {
	@Autowired
	private DeviceInfoMapper deviceInfoMapper;

	/**
	 * 保存终端
	 *
	 * @param deviceInfo TbDeviceInfo类型对象
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveDeviceInfo(TbDeviceInfo deviceInfo) {
		deviceInfoMapper.saveDeviceInfo(deviceInfo);
	}

	/**
	 * 根据Mac获取终端
	 * getDeviceInfoByMac
	 *
	 * @param deviceMac 终端mac
	 * @return TbDeviceInfo类型对象
	 */
	@Override
	public TbDeviceInfo getDeviceInfoByMac(String deviceMac) {
		return deviceInfoMapper.getDeviceInfoByMac(deviceMac);
	}

	/**
	 * 更新终端
	 * updateDeviceInfo
	 *
	 * @param deviceInfo    TbDeviceInfo类型对象
	 * @param oldDeviceInfo TbDeviceInfo类型对象
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceInfo(TbDeviceInfo deviceInfo, TbDeviceInfo oldDeviceInfo) {
		oldDeviceInfo.setDeviceResolution(deviceInfo.getDeviceResolution());
		oldDeviceInfo.setDeviceSoftVer(deviceInfo.getDeviceSoftVer());
		oldDeviceInfo.setDeviceModel(deviceInfo.getDeviceModel());
		oldDeviceInfo.setDeviceDiskSpace(deviceInfo.getDeviceDiskSpace());
		oldDeviceInfo.setDeviceIp(deviceInfo.getDeviceIp());
		//设置最新上报时间
		long currentTimeMillis = System.currentTimeMillis();
		oldDeviceInfo.setReportTime(currentTimeMillis);
		deviceInfoMapper.updateDeviceInfo(oldDeviceInfo);
	}
	
	/**
	 * 直接更新终端信息
	 * updateDeviceInfo
	 *
	 * @param deviceInfo    TbDeviceInfo类型对象
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void updateDeviceInfo(TbDeviceInfo deviceInfo) {
		deviceInfoMapper.updateDeviceInfo(deviceInfo);
	}
	 /**
     * 递归查询终端所属分组名称
     * @param groupCode 最小一级分组code码
     * @return
     */
	@Override
	public String queryGroupNamesByCodeRecursively(String groupCode) {
		if ( StringUtils.isBlank(groupCode) )  return null;
		return deviceInfoMapper.queryGroupNamesByCodeRecursively(groupCode);
	}
	
	/**
	 * 通过机器码获取设备信息（该信息目前存放在dsp中，将数据导入到了我们自己的库中）
	 * @param deviceNum 终端机器码
	 * @return TbMachine
	 */
	@Override
	public TbMachine getMachineByMachineNum(String deviceNum) {
		return deviceInfoMapper.getMachineByMachineNum(deviceNum);
	}

	@Override
	public List<DeviceGroupDTO> getAllGroups() {
		return deviceInfoMapper.getAllGroups();
	}

	@Override
	public List<String> getOnLineDeviceNumByGroup(String groupCode) {
		return deviceInfoMapper.getOnLineDeviceNumByGroup(groupCode);
	}

}
